package cc.rengu.igas.opms.common.util;

import cc.rengu.igas.opms.common.constant.OpmsParamConstant;
import cc.rengu.igas.opms.common.enums.RespCodeEnum;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;

import java.math.BigDecimal;

/**
 * 积分工具类
 * <p>
 * Created by keyi on 2020/3/25.
 */
public class IntegralUtil {

    /**
     * 积分兑换金额检查
     *
     * @param instId    机构号
     * @param transAmt  交易金额
     * @param transCore 兑换积分
     * @return 检查是否正确
     */
    public static boolean integralExchangeCheck(String instId, String transAmt, String transCore, String acctType) throws Exception {
        //获取兑换比例
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam integralExchangeScale = sysParamService.getSysParamInfo(instId, OpmsParamConstant.SCORE_RATE, acctType);
        if (integralExchangeScale == null) {
            //rglog.info("积分兑换规则未配置，instId:<{}>", instId);
            throw new BizException(RespCodeEnum.CHANNEL_CONFIG_NOT_EXIST.getRespCode(), RespCodeEnum.CHANNEL_CONFIG_NOT_EXIST.getRespDesc());
        }

        //积分转换金额
        BigDecimal exchangeAmt = new BigDecimal(transCore).multiply(new BigDecimal(integralExchangeScale.getParamValue()));

        //比较转换金额和交易金额
        if (new BigDecimal(transAmt).compareTo(exchangeAmt) == 0) {
            //rglog.debug("积分兑换正确,兑换积分数:<{}>,积分兑换比例:<{}>,上送交易金额:<{}>", transCore, integralExchangeScale.getParamValue(), transAmt);
            return true;
        } else {
            //rglog.debug("积分兑换错误,兑换积分数:<{}>,积分兑换比例:<{}>,上送交易金额:<{}>", transCore, integralExchangeScale.getParamValue(), transAmt);
            throw new BizException(RespCodeEnum.PARAM_ERROR.getRespCode(), RespCodeEnum.PARAM_ERROR.getRespDesc());
        }
    }

    /**
     * 金额转换为积分
     *
     * @param instId   机构号
     * @param amt      金额
     * @param acctType 卡类型
     * @return 积分
     * @throws Exception
     */
    public static String amtSwitchScore(String instId, String amt, String acctType) throws Exception {
        //获取兑换比例
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam integralExchangeScale = sysParamService.getSysParamInfo(instId, OpmsParamConstant.SCORE_RATE, acctType);
        if (integralExchangeScale == null) {
            //rglog.info("积分兑换规则未配置，instId:<{}>", instId);
            throw new BizException(RespCodeEnum.CHANNEL_CONFIG_NOT_EXIST.getRespCode(), RespCodeEnum.CHANNEL_CONFIG_NOT_EXIST.getRespDesc());
        }

        return new BigDecimal(amt).multiply(new BigDecimal(integralExchangeScale.getParamValue())).toString();
    }

    /**
     * 积分转换金额
     *
     * @param instId   机构号
     * @param score    积分
     * @param acctType 卡类型
     * @return 金额
     * @throws Exception
     */
    public static String scoreSwitchAmt(String instId, String score, String acctType) throws Exception {
        //获取兑换比例
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam integralExchangeScale = sysParamService.getSysParamInfo(instId, OpmsParamConstant.SCORE_RATE, acctType);
        if (integralExchangeScale == null) {
            //rglog.info("积分兑换规则未配置，instId:<{}>", instId);
            throw new BizException(RespCodeEnum.CHANNEL_CONFIG_NOT_EXIST.getRespCode(), RespCodeEnum.CHANNEL_CONFIG_NOT_EXIST.getRespDesc());
        }

        return new BigDecimal(score).divide(new BigDecimal(integralExchangeScale.getParamValue())).toString();
    }
}
